<template>
  <!-- 站点详细统计列表 -->
  <div>
    <v-page-search>
      <el-form :inline="true"
               :model="searchForm"
               solt="search-from">
        <el-form-item>
          <el-input v-model="searchForm.area"
                    placeholder="行政区划"
                    style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchForm.submittedName"
                    placeholder="上报单位"
                    style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchForm.submittedUnit"
                    placeholder="上报编码"
                    style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.territory"
                     multiple
                     collapse-tags
                     @change="getTableData"
                     style="width: 170px"
                     placeholder="责任区域">
            <el-option v-for="item in options.territory"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.isEnabled"
                     collapse-tags
                     style="width: 100px"
                     placeholder="启用">
            <el-option v-for="item in options.isEnabled"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="search-but">
        <el-button size="small"
                   type="primary"
                   icon="el-icon-search"
                   @click="search">搜索</el-button>
        <el-button size="small"
                   type="info"
                   icon="el-icon-delete"
                   @click="clearCondition">清除</el-button>
        <el-button size="small"
                   icon="el-icon-plus"
                   @click="add">添加</el-button>
        <!-- <el-button size="small"
                   type="warning"
                   icon="el-icon-document"
                   v-loading="exprotLoading"
                   @click="exprotInfo">导出</el-button> -->
      </div>
    </v-page-search>
    <!-- 表格 -->
    <el-table :data="tableData"
              @row-dblclick="edit"
              v-loading="loading"
              border
              style="width: 50%"
              stripe>
      <el-table-column prop="province"
                       label="省份"
                       sortable
                       width="90"
                       align="center"></el-table-column>
      <el-table-column prop="city"
                       label="市"
                       sortable
                       width="90"
                       align="center"></el-table-column>
      <el-table-column prop="county"
                       label="县"
                       sortable
                       width="90"
                       align="center"></el-table-column>
      <el-table-column prop="submittedName"
                       label="上报单位"
                       sortable
                       align="center"
                       width="120">
        <template slot-scope="props">
          <el-popover placement="bottom"
                      trigger="hover"
                      :content="props.row.submittedName">
            <span slot="reference"
                  class="table-overflow-hidden">{{props.row.submittedName}}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="submittedUnit"
                       label="上报编码"
                       align="center"></el-table-column>
      <el-table-column prop="territory"
                       label="责任区域"
                       sortable
                       align="center"></el-table-column>
      <el-table-column prop="isEnabled"
                       label="启用"
                       width="50"
                       align="center">
        <template slot-scope="props">
          <span v-if="props.row.isEnabled === 'FALSE'"
                class="table-background-color-red">否</span>
          <span v-else>是</span>
        </template>
      </el-table-column>
      <el-table-column label="操作"
                       width="50"
                       align="center"
                       v-if="userInfo.isEdit">
        <template slot-scope="scope">
          <el-button type="text"
                     size="mini"
                     @click="delReported(scope.row.submittedUnit)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination layout="total, sizes, prev, pager, next, jumper"
                   :total="pagging.total"
                   :page-sizes="pagging.sizes"
                   :current-page="pagging.currentPage"
                   @size-change="sizeChange"
                   @current-change="pageChange">
    </el-pagination>
    <!-- 弹出对话框 -->
    <client-dialog :dialog-visible="clientConfig.dialogVisible"
                   :client-info="clientConfig.clientInfo"
                   :mode="clientConfig.mode"
                   @close="onClose"></client-dialog>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { getReported, delReported } from '@/api/site'
import ClientDialog from './client-detail/client-dialog'

import _ from 'lodash'
export default {
  data() {
    return {
      loading: false,
      exprotLoading: false,
      // 配置信息
      clientConfig: {
        dialogVisible: false,
        clientInfo: {},
        mode: 1
      },
      // 筛选条件
      searchForm: {
        area: '',
        submittedName: '',
        submittedUnit: '',
        territory: [],
        isEnabled: ''
      },
      // 选项
      options: {
        // 责任区域
        territory: [],
        isEnabled: [
          {
            label: '',
            value: null
          },
          {
            label: '是',
            value: 'TRUE'
          },
          {
            label: '否',
            value: 'FALSE'
          }
        ]
      },
      // 表格数据
      tableData: [],
      // 分页属性
      pagging: {
        total: 0,
        currentPage: 1,
        pageSize: 50,
        sizes: [50, 100, 300, 500, 1000]
      }
    }
  },
  computed: {
    ...mapGetters([
      'appEnum',
      'userInfo'
    ])
  },
  mounted() {
    this.initEnum()
  },
  methods: {
    search() {
      this.pagging.currentPage = 1
      this.getTableData()
    },
    getTableData() {
      this.loading = true
      const params = _.assignIn({}, this.searchForm, {
        pageIndex: this.pagging.currentPage,
        pageSize: this.pagging.pageSize
      })
      // 多选条件处理
      params.territory = params.territory.join(',')

      getReported(params).then(res => {
        if (res.data.success) {
          const result = res.data.data
          this.tableData = result.data
          this.pagging.total = result.totalCount
          this.pagging.currentPage = result.pageIndex
          this.pagging.pageSize = result.pageSize
          this.loading = false
        }
      })
    },
    // 每页显示条数变更新
    sizeChange(size) {
      this.pagging.pageSize = size
      this.getTableData()
    },
    // 跳转到页面
    pageChange(curPage) {
      this.pagging.currentPage = curPage
      this.getTableData()
    },
    // 清除筛选条件
    clearCondition() {
      this.searchForm.area = ''
      this.searchForm.submittedName = ''
      this.searchForm.submittedUnit = ''
      this.searchForm.territory = []
      this.searchForm.isEnabled = null
      this.search()
    },
    // 编辑客户单位信息
    edit(row) {
      this.clientConfig.dialogVisible = true
      this.clientConfig.mode = 2
      this.clientConfig.clientInfo = row
    },
    // 新增
    add() {
      this.clientConfig.dialogVisible = true
      this.clientConfig.mode = 1
      this.clientConfig.clientInfo = {}
    },
    // 删除
    delReported(submittedUnit) {
      this.$confirm('此操作将永久删除该上报单位信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      }).then(() => {
        delReported({
          submittedUnit: submittedUnit
        }).then(res => {
          if (res.data.success) {
            const result = res.data.data
            if (result) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getTableData()
            }
          }
        })
      })
    },
    // 初始化下拉菜单
    initEnum() {
      this.options.territory = this.appEnum.territory
      setTimeout(() => {
        if (this.userInfo.department.indexOf('服务') !== -1) this.searchForm.territory.push(this.userInfo.department)
        this.search()
      }, 1000)
    },
    // 关闭对话框
    onClose() {
      this.clientConfig.dialogVisible = false
      this.getTableData()
    }
  },
  components: {
    ClientDialog
  }
}
</script>
